package com.demo.seckill.mybatis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/*")
public class UserController {

   @SuppressWarnings("all")
   @Autowired
   UserMapper userMapper;

   @Autowired
   UserInfoWithAddressMapper userInfoWithAddressMapper;

   @GetMapping("list")
   public List<UserInfo> list() {
       return userMapper.listSample();
   }

   @GetMapping("list/{name}")
   public List<UserInfo> listByUsername(@PathVariable("name") String name) {
       return userMapper.listByName(name);
   }

   @GetMapping("get/{name}/{sex}")
   public UserInfo get(@PathVariable("name") String name, @PathVariable("sex") String sex) {
       return userMapper.get(name, sex);
   }

   @GetMapping("get/bad/{name}/{sex}")
   public List<UserInfo> getBadUser(@PathVariable("name") String name, @PathVariable("sex") String sex) {
       return userMapper.getBadUser(name, sex);
   }

    @GetMapping("insert/{name}/{age}/{sex}")
    public boolean insert(@PathVariable("name") String name, @PathVariable("age")int age,@PathVariable("sex") String sex) {
       UserInfo userInfo = new UserInfo(name,age,sex);
        //return userMapper.insertUserInfo(name,age,sex)>0 ?true: false;
        return userMapper.insertUserInfo2(userInfo) >0 ?true: false;
    }

    @GetMapping("updateUser/{name}/{id}")
    public int update(@PathVariable("name") String name, @PathVariable("id")int id) {
        //UserInfo userInfo = new UserInfo(name,age,sex);
        //return userMapper.insertUserInfo(name,age,sex)>0 ?true: false;
        return userMapper.updateUserInfo(name,id);
    }

    @GetMapping("deleteUser/{name}")
    public int delete(@PathVariable("name") String name) {
        //UserInfo userInfo = new UserInfo(name,age,sex);
        //return userMapper.insertUserInfo(name,age,sex)>0 ?true: false;

        //return userMapper.deleteUserInfo(name);
        UserInfo userInfo = new UserInfo();
        userInfo.setName(name);
        return userMapper.deleteUserInfo2(userInfo);
    }


    @GetMapping("getUser/{id}")
    public UserInfo getUser(@PathVariable("id") int id) {
        return userMapper.getUserWithCar(id);
        //return  userMapper.getUserWithAddress(id);
    }

    @GetMapping("getUserWithAddress/{id}")
    public UserInfoWithAddress getUserWithAddress(@PathVariable("id") int id) {
        return userInfoWithAddressMapper.getUser(id);
    }


}